<?php
/**
 * $Id: AdminDAL.mod.php 773 2008-07-01 06:19:05Z dakl $
 * Klasa do obs�ugi odwo�a� do bazy danych:
 * - pobieranie danych
 * - usuwanie
 * - dodawanie
 * - edycja
 * - modyfikacja has�a
 *
 */
class AdminDAL {

	/**
	 * Pusty konstruktor
	 *
	 */
	public function __construct() {

	}

	/**
	 * Usuni�cie rekordu/obiektu z bazy
	 *
	 * @param int $id
	 */
	public function Delete($id) {
		$db = Registry::Get('db');
		$sql = "DELETE FROM mf_admin WHERE id_mf_admin=:1";
		$stmt=$db->prepare($sql)
		->bindParam(1, $id)
		->execute();
	}
	/**
	 * Pobranie obiektu na podstawie ID
	 *
	 * @param int $id
	 */
	public function GetObjById($id) {
		$db = Registry::Get('db');
		$sql = "SELECT * from mf_admin where id_mf_admin=:1";
		$stmt=$db->prepare($sql)
		->bindParam(1, $id)
		->execute();
		return $stmt->fetchAssoc();
	}
	/**
	 * Pobranie wszystkich obiekt�w/rekord�w
	 *
	 */
	public function GetArrayObjAll() {
		$db = Registry::Get('db');
		$sql = "SELECT * from mf_admin";
		$stmt=$db->execute($sql);
		return $stmt->fetchAllAssoc();
	}

	/**
	 * Pobieranie listy rol
	 *
	 * @return array
	 */
	public function GetArrayObjRoles() {
		
		$db = Registry::Get('db');
		$sql= "SELECT DISTINCT role from mf_admin_access";
		$stmt=$db->execute($sql);
		return $stmt->FetchAllAssoc();
	}

        /**
	 * Pobieranie listy rol
	 *
	 * @return array
	 */
        public function GetArrayObjUserRoles()
        {
            $db = Registry::Get('db');
            $sql= "SELECT DISTINCT * from role";
            $stmt=$db->execute($sql);
            return $stmt->FetchAllAssoc();
        }
        /**
         * rejestracja użytkownika
         * @param <Array> $params
         */
        public function InsertMfUser($params)
        {
            $db = Registry::Get('db');
           
            $sql = "INSERT INTO mf_user(login, password, email, id_role) values (:1, :2, :3, :4)";
            $stmt=$db->prepare($sql)
		->bindParam(1, $params['login'])
		->bindParam(2, md5($params['password']).':1')
		->bindParam(3, $params['email'])
		->bindParam(4, $params['role'])
		->execute();
        }

	/**
	 * Aktualizacja uzytkownika po id
	 *
	 * @param integer $id
	 * @param string $name
	 * @param string $email
	 * @param string $role
	 */
	public function UpdateObjById($id, $name, $email, $role) {
		$db = Registry::Get('db');
		$sql = "UPDATE mf_admin set first_name=:1, email=:2, role=:3 WHERE id_mf_admin=:4";
		$stmt=$db->prepare($sql)
		->bindParam(1, $name)
		->bindParam(2, $email)
		->bindParam(3, $role)
		->bindParam(4, $id)
		->execute();
	}

	/**
	 * Zmiana hasla
	 *
	 * @param integer $id
	 * @param string $password
	 * @param string $oldPassword
	 */
	public function UpdatePassword($id, $password, $oldPassword = null) {
		$db = Registry::Get('db');
		$sql = "UPDATE mf_admin set password=:1 WHERE id_mf_admin=:2";
		$stmt=$db->prepare($sql)
		->bindParam(1, md5($password))
		->bindParam(2, $id)
		//->bindParam(3, $oldPassword)
		->execute();
	}
	
	public function CheckPassword($id, $password) {
		$db = Registry::Get('db');
		$sql = "select count(*) from mf_admin WHERE password=:1 AND id_mf_admin=:2";
		$stmt=$db->prepare($sql)
		->bindParam(1, $password)
		->bindParam(2, $id)
		->execute();
		
		$result = $stmt->FetchRow();
		
		return $result[0];
		
	}

	/**
	 * Dodanie nowego uzytkownika
	 *
	 * @param string $login
	 * @param string $name
	 * @param string $password
	 * @param string $email
	 * @param string $role
	 */
	public function Insert($login, $name, $password, $email, $role) {
		$db = Registry::Get('db');
		$sql = "INSERT INTO mf_admin (login, password, first_name, email, role) values (:1, :2, :3, :4, :5)";
		$stmt=$db->prepare($sql)
		->bindParam(1, $login)
		->bindParam(2, $password)
		->bindParam(3, $name)
		->bindParam(4, $email)
		->bindParam(5, $role)
		->execute();
	}


}
?>